﻿using Aspose.Cells;
using CefSharp;
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static DouYin.Form1.VideoInfo;

namespace DouYin
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.chromiumWebBrowser1.LoadUrl("http://www.douyin.com");
        }

        string htmlContent = "";

        private async Task<string> GetHtmlContent(string url)
        {
            this.chromiumWebBrowser1.LoadUrl(url);
            
            var result = await this.chromiumWebBrowser1.WaitForInitialLoadAsync();
            var len = new Random().Next(1, 10);
            for (int i = 0; i < len; i++)
            {
                Thread.Sleep(500);
                await this.chromiumWebBrowser1.EvaluateScriptAsync($"window.scrollTo(0, {i * 600})");
            }

            if (result.Success)
            {
                Thread.Sleep(1000);
                return await this.chromiumWebBrowser1.GetSourceAsync();
            }
            return "";

            //var dlg = new Spider(url);
            //dlg.Show();
            //string content = await dlg.GetHtmlContent();
            //dlg.Close();
            //return content;
        }

        private async void button1_ClickAsync(object sender, EventArgs e)
        {

            htmlContent = await GetHtmlContent("https://www.douyin.com/user/MS4wLjABAAAAkRGOj6TK4OaUun8JHl9hBfBZMOcwVlXqa9ScEHVHNfw");
            // this.richTextBox1.Text = content;
        }


        List<VideoInfo> lstVideo = new List<VideoInfo>();
        private void button2_Click(object sender, EventArgs e)
        {
            HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
            document.LoadHtml(htmlContent);
            // 获取视频标题和url
            HtmlNodeCollection liItems = document.DocumentNode.SelectNodes("//a[@class='hY8lWHgA SF0P5HVG h0CXDpkg']");
            foreach (HtmlNode li in liItems)
            {

                var url = li.GetAttributeValue("href", "");

                var title = li.ChildNodes.FirstOrDefault(n => n.Name == "p")?.InnerHtml;

                lstVideo.Add(new VideoInfo
                {
                    title = CleanHtml(li.InnerHtml),
                    url = url,
                });
              //  this.richTextBox2.Text += $"{url} {title}" + System.Environment.NewLine;
            }
            //foreach (var videoInfo in lstVideo)
            //{
            //    var tempurl = $"https://www.douyin.com{videoInfo.url}";

            //    var dlg = new Spider(tempurl);
            //    dlg.Show();
            //    string content = dlg.GetHtmlContent().Result;
            //    dlg.Close();
            //}

        }

        private async void button3_Click(object sender, EventArgs e)
        {
            var tempurl = $"https://www.douyin.com/video/7135238963953995011";
            var content = await GetHtmlContent(tempurl);
            //  writefile(content);

            // var content = ReadFile();


            HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
            document.LoadHtml(content);

            var commentList = document.DocumentNode.SelectNodes("//div[@data-e2e='comment-item']/div[2]/div");


            foreach (HtmlNode comment in commentList)
            {
                foreach (var tempNode in comment.ChildNodes.Where(n => n.Name == "div"))
                {
                    var className = tempNode.GetAttributeValue("class", "");
                    if (className == "oUPTYnUM comment-item-info-wrap") //名称
                    {
                        foreach (var item in tempNode.ChildNodes.Where(n => n.Name == "div"))
                        {
                            if (item.GetAttributeValue("class", "") == "b2riW_HJ")
                            {
                                foreach (var qtem in item.ChildNodes.Where(n => n.Name == "div"))
                                {
                                    if (qtem.GetAttributeValue("class", "") == "ax6MlHvK")
                                    {
                                        var xxxa = qtem.ChildNodes.FirstOrDefault(n => n.Name == "a");
                                       // this.richTextBox1.Text += xxxa.GetAttributeValue("href", "") + " ";
                                      //  this.richTextBox1.Text += CleanHtml(xxxa?.InnerHtml) + System.Environment.NewLine;
                                    }
                                }
                            }
                        }
                        // this.richTextBox1.Text += tempNode.InnerHtml + System.Environment.NewLine;

                    }
                    else if (className == "LvAtyU_f") //评论 
                    {
                        var commentNode = tempNode.ChildNodes?.FirstOrDefault(n => n.Name == "span");
                        if (commentNode != null)
                        {
                           // this.richTextBox1.Text += CleanHtml(commentNode.InnerHtml) + System.Environment.NewLine;
                        }
                    }

                    else if (className == "GOkWHE6S") //评论 
                    {
                       // this.richTextBox1.Text += CleanHtml(tempNode.InnerHtml) + System.Environment.NewLine;
                    }
                }

                //break;
            }


        }
        /// <summary>
        /// 去掉HTML中的所有标签,只留下纯文本
        /// </summary>
        /// <param name="strHtml"></param>
        /// <returns></returns>

        public string CleanHtml(string Htmlstring)
        {
            //删除脚本
            Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            //删除HTML
            Htmlstring = Regex.Replace(Htmlstring, @"<br>", "$br$", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"$br$", "<br>", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "<br>", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            // Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
            Htmlstring.Replace("<", "");
            Htmlstring.Replace(">", "");
            Htmlstring.Replace("\r\n", "");
            return Htmlstring;
        }


        private void WriteFile(string content)
        {

            if (System.IO.File.Exists("temp.html"))
            {
                System.IO.File.Delete("temp.html");
            }
            using (StreamWriter writer = new StreamWriter("temp.html"))
            {
                writer.WriteLine(content); // 写入一行文本
            }
        }

        private string ReadFile()
        {
            var content = "";
            using (StreamReader reader = new StreamReader("temp.html"))
            {
                content = reader.ReadToEnd();
            }
            return content;
        }

        private async void export_Click(object sender, EventArgs e)
        {
            List<VideoInfo> lstVideo = new List<VideoInfo>();
            var htmlContent = await GetHtmlContent($"https://www.douyin.com/user/{this.textBox1.Text}");

            HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
            document.LoadHtml(htmlContent);
            // 获取视频标题和url
            HtmlNodeCollection liItems = document.DocumentNode.SelectNodes("//a[@class='hY8lWHgA SF0P5HVG h0CXDpkg']");
            foreach (HtmlNode li in liItems)
            {

                var url = li.GetAttributeValue("href", "");

                var title = li.ChildNodes.FirstOrDefault(n => n.Name == "p")?.InnerHtml;
                if (url.Contains("video"))
                {
                    lstVideo.Add(new VideoInfo
                    {
                        title = CleanHtml(li.InnerHtml),
                        url = url,
                    });
                } 

            }
            int d = 0;
            foreach (VideoInfo li in lstVideo)
            {
                Thread.Sleep(3000);
                var tempurl = $"https://www.douyin.com{li.url}";
                var content = await GetHtmlContent(tempurl);
                document.LoadHtml(content);
                var commentList = document.DocumentNode.SelectNodes("//div[@data-e2e='comment-item']/div[2]/div");

                if (commentList != null)
                {
                    foreach (HtmlNode comment in commentList)
                    {
                        var ci = new CommentInfo();
                        foreach (var tempNode in comment.ChildNodes.Where(n => n.Name == "div"))
                        {

                            var className = tempNode.GetAttributeValue("class", "");
                            if (className == "oUPTYnUM comment-item-info-wrap") //名称
                            {
                                foreach (var item in tempNode.ChildNodes.Where(n => n.Name == "div"))
                                {
                                    if (item.GetAttributeValue("class", "") == "b2riW_HJ")
                                    {
                                        foreach (var qtem in item.ChildNodes.Where(n => n.Name == "div"))
                                        {
                                            if (qtem.GetAttributeValue("class", "") == "ax6MlHvK")
                                            {
                                                var xxxa = qtem.ChildNodes.FirstOrDefault(n => n.Name == "a");
                                                ci.url = xxxa.GetAttributeValue("href", "") + " ";
                                                ci.userName = CleanHtml(xxxa?.InnerHtml);
                                            }
                                        }
                                    }
                                }
                            }
                            else if (className == "LvAtyU_f") //评论 
                            {
                                var commentNode = tempNode.ChildNodes?.FirstOrDefault(n => n.Name == "span");
                                if (commentNode != null)
                                {
                                    ci.content = CleanHtml(commentNode.InnerHtml) + System.Environment.NewLine;
                                }
                            }

                            else if (className == "GOkWHE6S") //评论 
                            {
                                ci.Time = CleanHtml(tempNode.InnerHtml) + System.Environment.NewLine;
                            }
                        }

                        li.comments.Add(ci);

                    }
                }
                d += 1;

                //if (d > 2)
                //{
                //    break;
                //}
            }

            Workbook workbook = new Workbook();
            workbook.Worksheets.Clear();
            Worksheet sheet = workbook.Worksheets.Add("sheet");

            Aspose.Cells.Style style = workbook.CreateStyle();
            // 设置居中
            style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;
            // 设置背景颜色
            style.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0);
            style.Pattern = Aspose.Cells.BackgroundType.Solid;
            style.Font.IsBold = true;
            int rowIndex = 0;
            for (int i = 0; i < cols.Length; i++)
            {
                style.Font.Size = 10;
                sheet.Cells[rowIndex, i].PutValue(cols[i]);
                sheet.Cells[rowIndex, i].SetStyle(style);
                sheet.Cells.SetColumnWidth(i, 15); //设置列宽
            }
            rowIndex++;
         
            foreach (var row in lstVideo)
            {
                sheet.Cells[rowIndex, 0].PutValue(row.url.ToString());
                sheet.Cells[rowIndex, 1].PutValue(row.title.ToString());

                foreach (var comment in row.comments)
                {
                    if (!string.IsNullOrEmpty(comment.url))
                    {
                        sheet.Cells[rowIndex, 2].PutValue("http:"+comment.url ?? "");
                        sheet.Cells[rowIndex, 3].PutValue(comment.userName ?? "");

                        sheet.Cells[rowIndex, 4].PutValue(comment.content ?? "");
                        sheet.Cells[rowIndex, 5].PutValue(comment.Time ?? "");
                        rowIndex++;
                    }
                }
               

            }
            workbook.Save("export.xlsx");

            MessageBox.Show("导出完成");

        }

        public string[] cols = new string[] { "视频地址", "标题", "个人主页", "个人昵称", "评论", "时间" };


        public class VideoInfo
        {
            public VideoInfo()
            {
                comments = new List<CommentInfo> { };
            }
            public string url { get; set; }

            public string title { get; set; }

            public List<CommentInfo> comments { get; set; }


            public class CommentInfo
            {

                public string url { get; set; }

                public string userName { get; set; } //a 标签的 outhtml

                public string content { get; set; } // LvAtyU_f

                public string Time { get; set; }
            }

        }
    }
}
